package com.zhongge.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhongge.entity.Holiday;
import com.zhongge.service.impl.HolidayServiceImpl;
import com.zhongge.vo.HolidayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhongge
 * @since 2025-11-09
 */
@Controller
@RequestMapping("/holiday")
public class HolidayController {
    @Autowired
    private HolidayServiceImpl holidayService;

    //分页显示所有的假期信息
    @ResponseBody
    @GetMapping("/list")
    public Map<String, Object> pageShowHolidayInfo(@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer size){
        Map<String, Object> res = new HashMap<>();
        try{
            IPage<HolidayVO> holidayVOIPage = holidayService.pageShowHolidayInfo(page, size);
            res.put("rows",holidayVOIPage.getRecords());
            res.put("total",holidayVOIPage.getTotal());
            res.put("success",true);
        }catch (Exception e){
            res.put("success",false);

            res.put("msg","查询失败"+e.getMessage());
        }
        return res;
    }


    //跳转到假期发布页面
    @GetMapping("/holidayPublish")
    public String dispatchHolidayPublish(){
        return "studestsys/teacher/holiday-publish";
    }
    //跳转到报表
    @GetMapping("/statisticsReport")
    public String dispatchStatisticsReport(){
        return "studestsys/teacher/statistics-report";
    }

    //跳转到数据导出
    @GetMapping("/dataExport")
    public String dispatchDataExport(){
        return "studestsys/teacher/data-export";
    }

    //根据假期id获取假期信息
    @GetMapping("/get/{id}")
    @ResponseBody
    public Holiday getHolidayInfoById(@PathVariable Integer id){
        return holidayService.getById(id);
    }

    //保存或者修改假期信息
    @ResponseBody
    @PostMapping("/save")
    public Map<String, Object> saveOrUpdateHoliday(Holiday holiday){
        Map<String, Object> res = new HashMap<>();
        try{
            boolean b = holidayService.saveOrUpdate(holiday);
            if (b){
                res.put("success",true);
                res.put("message","新增或者修改成功✔️");
            }else {
                res.put("success",false);
                res.put("message","新增或者修改失败✖️");
            }

        }catch (Exception e){
            res.put("success",false);

            res.put("message","出现异常，请重试");
        }
        return res;
    }

    //发布假期
    @ResponseBody
    @PostMapping("/publish/{id}")
    public Map<String, Object> publish(@PathVariable Integer id){
        Map<String, Object> res = new HashMap<>();
        try{
            //首先查出该假期
            Holiday holiday = holidayService.getById(id);
            if (holiday!=null){
                //那么就将状态修改为1--正在进行
                holiday.setState(1);

                //最后将假期修改
                boolean b = holidayService.updateById(holiday);
                if (b){
                    res.put("success",true);
                    res.put("message","发布成功✔️");
                }else {
                    res.put("success",false);
                    res.put("message","发布失败✖️");
                }
            }else {
                res.put("success",false);
                res.put("message","该假期不存在");
            }
        }catch (Exception e){
            res.put("success",false);

            res.put("message","出现异常，请重试");
        }
        return res;
    }

    //撤回发布的假期
    @ResponseBody
    @PostMapping("/cancel/{id}")
    public Map<String, Object> cancel(@PathVariable Integer id){
        Map<String, Object> res = new HashMap<>();
        try{
            //首先查出该假期
            Holiday holiday = holidayService.getById(id);
            if (holiday!=null){
                //那么撤回就是就将状态修改变为0--待发布
                holiday.setState(0);

                //最后将假期修改
                boolean b = holidayService.updateById(holiday);
                if (b){
                    res.put("success",true);
                    res.put("message","撤回成功✔️");
                }else {
                    res.put("success",false);
                    res.put("message","撤回失败✖️");
                }
            }else {
                res.put("success",false);
                res.put("message","该假期不存在");
            }
        }catch (Exception e){
            res.put("success",false);

            res.put("message","出现异常，请重试");
        }
        return res;
    }

    //删除假期
    @ResponseBody
    @PostMapping("/delete")
    public Map<String, Object> delete(Integer id){
        Map<String, Object> res = new HashMap<>();
        try{
            boolean b = holidayService.removeById(id);
            if (b){
                res.put("success",true);
                res.put("message","删除成功✔️");
            }else {
                res.put("success",false);
                res.put("message","删除失败✖️");
            }
        }catch (Exception e){
            res.put("success",false);
            System.out.println(e.getMessage());
            res.put("message","可能该假期关联其他表 所以无法删除");
        }
        return res;
    }



    //查询假期根据id查询是否存在该假期
    @GetMapping("/registration/get/{state}")
    @ResponseBody
    public Holiday getHolidayByState(@PathVariable Integer state){
        QueryWrapper<Holiday> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",state);
        return holidayService.getOne(queryWrapper);
    }

}
